package com.owen.springboot.smartparkingms.module.sys.controller;


import com.owen.springboot.smartparkingms.common.constant.SystemConstant;
import com.owen.springboot.smartparkingms.common.model.Result;
import com.owen.springboot.smartparkingms.common.util.DateUtils;
import com.owen.springboot.smartparkingms.common.util.MD5Utils;
import com.owen.springboot.smartparkingms.common.util.ShiroUtils;
import com.owen.springboot.smartparkingms.module.sys.entity.SysUser;
import com.owen.springboot.smartparkingms.module.sys.entity.UserRole;
import com.owen.springboot.smartparkingms.module.sys.service.ISysUserService;
import com.owen.springboot.smartparkingms.module.sys.service.IUserRoleService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author Owen
 * @since 2022-02-25
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IUserRoleService userRoleService;

    /**
     * 用户列表
     */
    @RequestMapping("/list")
    public Result list(SysUser user){
        return userService.listPage(user);
    }

    /**
     * 获取
     */
    @PostMapping("/get")
    public Result get(Long userId){
        SysUser user = userService.getById(userId);
        return Result.ok(user);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresRoles("admin")
    public Result save(@RequestBody SysUser user){
        SysUser entity = userService.getUser(user.getUsername());
        if (entity != null){
            if(!entity.getUserId().equals(user.getUserId())){
                if(user.getUsername().equals(entity.getUsername())){
                    return Result.error("用户名重复！");
                }
            }
            user.setGmtModified(DateUtils.getTimestamp());
        }else {
            user.setUserIdCreate(ShiroUtils.getUserId());
            user.setGmtCreate(DateUtils.getTimestamp());
            user.setPassword(MD5Utils.encrypt(user.getUsername(), user.getPassword()));
            user.setAvatarStatus(SystemConstant.AVATAR_STATUS_NO);
        }
        userService.saveOrUpdate(user);
        userRoleService.deleteUserRole(user.getUserId());
        List<Object> roleList = user.getRoleIdList();
        if(roleList!=null){
            roleList.forEach(roleId->{
                UserRole userRole = new UserRole();
                userRole.setUserId(user.getUserId());
                userRole.setRoleId(Long.parseLong(roleId.toString()));
                userRoleService.save(userRole);
            });
        }
        return Result.ok("保存成功");
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresRoles("admin")
    public Result delete(Long userId){
        return userService.deleteById(userId);
    }

    /**
     * 修改密码
     */
    @PostMapping("/updatePwd")
    @RequiresRoles("admin")
    public Result updatePwd(SysUser user){
        String password = MD5Utils.encrypt(user.getUsername(),user.getPassword());
        SysUser sysUser = new SysUser();
        sysUser.setUserId(user.getUserId());
        sysUser.setPassword(password);
        sysUser.setIsModifyPwd(1);
        userService.updateById(sysUser);
        return Result.ok();
    }

    /**
     * 获取当前用户信息
     */
    @PostMapping("/info")
    public Result info(){
        SysUser user = userService.getById(ShiroUtils.getUserId());
        return Result.ok(user);
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/update")
    public Result update(@RequestBody SysUser user){
        userService.updateById(user);
        return Result.ok();
    }
}
